package bg.bgfirst;

import java.io.InputStream;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActivityAlertPhone extends Activity {

	private String numero;
	private String name;
	private String message;
	private TextView textViewCallerNumber;
	private TextView textViewCallerName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v("BG", "ActivityAlertPhone.oncreate1");
		setContentView(R.layout.activity_alert_phone);

		textViewCallerNumber = (TextView) findViewById(R.id.textViewCallerNumber);
		textViewCallerName = (TextView) findViewById(R.id.textViewContactName);
		message = this.getIntent().getStringExtra(IncomingCallReciever.KEY_MESSAGE_ALERT_CALL);
		numero = this.getIntent().getStringExtra(IncomingCallReciever.KEY_NUMERO_CALLER_);
		name = this.getContactNameFromNumber(numero);
		
		Log.v("BG", "ActivityAlertPhone.oncreate2 " + numero);
		Log.v("BG", "ActivityAlertPhone.oncreate2 " + message);
		textViewCallerNumber.setText(""+numero);
		textViewCallerName.setText(name);
		boolean isBlackListed = getApplicationBg().isBlackListed("" + numero);
		Button buttonAddToBlackList = (Button) findViewById(R.id.buttonAddToBlackList);
		OnClickListener buttonListener2 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.v("BG", "Add Black List!!!!!" + numero);
				textViewCallerNumber.setText("Blaklisted  " + numero);
				String number = numero;
				
				getApplicationBg().gethBlackList().put("" + number,number+" "+name);
				displayMainView();
				getApplicationBg().persist();
			}
		};
		buttonAddToBlackList.setOnClickListener(buttonListener2);

		Button buttonRemoveFromBlackList = (Button) findViewById(R.id.buttonRemoveFromBlackList);
		buttonRemoveFromBlackList.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.v("BG", "Remove FromBlackList " + numero);
				String number = numero;
				
				getApplicationBg().gethBlackList().remove("" + number);
				displayMainView();
				getApplicationBg().persist();
			}
		});

		buttonAddToBlackList.setEnabled(!isBlackListed);
		
		buttonRemoveFromBlackList.setEnabled(isBlackListed);
		if (isBlackListed){
			buttonRemoveFromBlackList.setBackgroundColor(Color.GRAY);
			buttonRemoveFromBlackList.setFocusableInTouchMode(true);
			buttonRemoveFromBlackList.requestFocus();
		}else {
			buttonAddToBlackList.setBackgroundColor(Color.GRAY);
			buttonAddToBlackList.setFocusableInTouchMode(true);
			buttonAddToBlackList.requestFocus();
		}
	}  

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_alert_phone, menu);
		return true;
	}

	private void displayMainView() {
		Intent intent = new Intent(this, ActivityMain.class);
		startActivity(intent);
	}

	public ApplicationBg getApplicationBg() {
		return (ApplicationBg) getApplication();
	}


	private   String getContactNameFromNumber( String number) {
		String name = null;
		String contactId = null;
		Log.v("bg2", "getContactNameFromNumber start");
	    // define the columns I want the query to return
		String[] projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID };

		// encode the phone number and build the filter URI
		Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

		// query time
		//Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
		Cursor cursor = this.getContentResolver().query(contactUri, projection, null, null, null);

		if (cursor.moveToFirst()) {

			// Get values from contacts database:
			contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
			name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));

			Log.v("bg2", "Started uploadcontactphoto: Contact Found @ " + number);
			Log.v("bg2", "Started uploadcontactphoto: Contact name  = " + name);
			Log.v("bg2", "Started uploadcontactphoto: Contact id    = " + contactId);

		} else {
			Log.v("bg2", "Started uploadcontactphoto: Contact Not Found @ " + number);
		}
		if(name == null){
			name="";
		}
		Log.v("bg2", "id = " + contactId + " name = " + name);

		
		return name;
	}

}
